import sys
import click

from .library.logger import config_logging
from .library.coverage_helper import setup_coverage
from .config import Config
from .task import Task, TaskExecutor
from .supervisor import Supervisor


@click.group()
def main():
    """TinyOps CLI"""
    setup_coverage()
    config_logging()


@main.command()
@click.option('--config')
def start(config):
    su = Supervisor(config_filepath=config)
    su.main()


@main.command()
@click.option('--task', type=click.File(), required=True)
@click.option('--config', type=click.File(), required=True)
def execute(task, config):
    """execute task"""
    config = Config.from_json(config.read())
    task = Task.from_json(task.read(), config=config)
    executor = TaskExecutor(task)
    is_ok = executor.execute()
    sys.exit(0 if is_ok else 1)


if __name__ == "__main__":
    main()
